﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.Position
{
    /// <summary>
    /// ChangePosition 的摘要说明。
    /// </summary>
    public partial class ChangePositionPage : Page
    {
        private string backfilepath;
        private string displayType;
        private string PositionID;
        private IList<int> selectedIDs;
        private int ReturnPage;
        private string Username;

        protected override void OnInit(EventArgs e)
        {
            cmdSubmit.ServerClick += cmdSubmit_ServerClick;
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            Username = SiteContext.Current.UserName;
            PositionID = (Request.QueryString["PositionID"] == null) ? "" : Request.QueryString["PositionID"];
            displayType = (Request.QueryString["displayType"] == null) ? "" : Request.QueryString["displayType"];
            backfilepath = (Request.QueryString["BackFilePath"] == null) ? "" : Request.QueryString["BackFilePath"];
            if (Request.QueryString["ReturnPage"] != null)
            {
                ReturnPage = Int32.Parse(Request.QueryString["ReturnPage"]);
            }
            else
            {
                ReturnPage = 0;
            }

            var str = Request.QueryString["StaffIDS"];
            if (!string.IsNullOrEmpty(str))
            {
                // Convert the string to an int array.
                var stringArray = str.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var intArray = Array.ConvertAll(stringArray, s => int.Parse(s));
                selectedIDs = new List<int>(intArray).AsReadOnly();
            }

            if (!Page.IsPostBack)
            {
                var db = new Database();
                using (SqlDataReader dr = db.RunDataReader("dbo.sproc_Ext_GetPosition"))
                {
                    cboPosition.DataSource = dr;
                    cboPosition.DataTextField = "Position_Name";
                    cboPosition.DataValueField = "Position_ID";
                    cboPosition.DataBind();
                }
            }

            base.OnLoad(e);
        }

        //调职通知
        private void sms_all()
        {
            var sm = new SMS();
            IList<StaffInfo> allStaff = Staff.GetAllStaff(false);
            List<string> staffToNotify = new List<string>(allStaff.Count);
            foreach (StaffInfo staff in allStaff)
            {
                staffToNotify.Add(staff.Name);
            }
            string[] array = staffToNotify.ToArray();

            foreach (var selectedStaff in Staff.GetStaffInfo(selectedIDs))
            {
                SMS.SendLocalMsg(
                    Username,
                    array,
                    selectedStaff.PositionName + "处员工:" + selectedStaff.RealName + ",已经调职到" + cboPosition.Items[cboPosition.SelectedIndex].Text + "处,特此通知.");
            }
        }

#if false
        private void sms_all(int i)
        {
            SqlDataReader dr_this; //被选择人员
            var sta = new Components.Staff();
            dr_this = sta.GetStaffInfo(selectedID);
            var sm = new SMS();
            //处理短信提醒
            while (dr_this.Read())
            {
                string Position_name = dr_this["Position_name"].ToString();
                SqlDataReader dr_isok; //所有在职人员
                dr_isok = sta.GetAllStaffs();
                while (dr_isok.Read())
                {
                    string Staff_name = dr_isok["Staff_name"].ToString();
                    if (i == 0)
                        sm.SendMsg(Username, Staff_name, Position_name + " 处员工:" + dr_this["RealName"] + ",已经离职,特此通知.", 1, DateTime.Now, "", 0, 0);
                    else
                        sm.SendMsg(Username, dr_isok["Staff_name"].ToString(), dr_this["Position_name"] + " 处员工:" + dr_this["RealName"] + ",已经恢复原职,特此通知.", 1, DateTime.Now, "", 0, 0);
                }
                dr_isok.Close();
                dr_isok = null;
            }
            sm = null;
            dr_this.Close();
            dr_this = null;
        }
#endif

        private void cmdSubmit_ServerClick(object sender, EventArgs e)
        {
            if (cbRemind.Checked)
            {
                sms_all();
            }
            var db = new Database();
            var xml = Components.Staff.GenerateStaffXML(selectedIDs);
            var positionId = Int32.Parse(cboPosition.Items[cboPosition.SelectedIndex].Value);
            var prams = new[]
            {
                Database.MakeInParam("@xml", SqlDbType.Xml, -1, xml),
                Database.MakeInParam("@NewPositionID", SqlDbType.Int, 4, positionId)
            };
            db.RunProc("dbo.sproc_StaffMove", prams);
            if (ReturnPage == 0)
            {
                Response.Redirect(backfilepath + "?PositionID=" + PositionID + "&displayType=" + displayType);
            }
            else
            {
                Response.Redirect("../Staff/ManageStaff.aspx?DisplayType=0");
            }
        }
    }
}